/**
 * 用户管理（添加|修改）userOption.js
 * 版本号： v1.0.0
 */
$(function(){
    //设置输入框的最大输入长度
    $("#loginName").textbox("textbox").attr("maxlength", 18);
    $("#userPhone").textbox("textbox").attr("maxlength", 11);
    $("#remark").textbox("textbox").attr("maxlength", 202);
    var isAdd = $("#usId").val();

    //所属公司下拉树
    $.ajax({
        url: CONTEXT_PATH + "/company/getTreeCompany.do",
        type: "get",
        success: function (data) {
            var themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
            themecombo += data.substring(1);
            themecombo = JSON.parse(themecombo);
            $("#companyTree").data("companyList", themecombo);
            $("#companyTree").combotree("loadData",themecombo);
            $("#companyTree").combotree({
                onChange:function(node){
                    $('#companyId').val(node);
                    //在选择后根据选择公司的ID去查询员工下拉框
                    var companyId = $('#companyId').val();
                    $("#employeeSelect").combobox("loadData",{});
                    $.ajax({
                        url: CONTEXT_PATH + "/employee/getEmployeeList.do",
                        data:{
                            "companyId":companyId
                        },
                        type: "get",
                        success: function (data) {
                            var themecombo = JSON.parse(data);
                            $("#employeeSelect").combobox("loadData",themecombo);
                            $("#employeeSelect").combobox({
                                valueField:'id',
                                textField:'text',
                                onChange:function(node){
                                    $('#emId').val(node);
                                }
                            });
                            if(!isAdd || isAdd == ""){
                                $("#employeeSelect").combobox("setValue","");
                            }else{
                                var usId = $("#usId").val();
                                var companyId = $("#companyId").val();
                                var oldCompanyId = $("#oldCompanyId").val();
                                if(oldCompanyId == companyId){
                                    $("#employeeSelect").combobox("setValue",usId);
                                }
                            }
                        }
                    });
                }
            });
            //修改操作时，数据加载完成后给各个选框赋值
            setValue();
        }
    });

    //设置角色勾选
    $.ajax({
        url: CONTEXT_PATH + "/role/getRoleList.do",
        type: "get",
        success: function (data) {
            data = data.replace(/roId/g,"id").replace(/name/g,"text");
            if(data.length > 2){
                data = JSON.parse(data);
            }
            $("#roleCheckbox").tree({
                data:data,
                checkbox:true,
                onLoadSuccess:function(){
                    var roleIds = $("#roleIds").val();
                    if(!roleIds){
                        return;
                    }
                    roleIds = roleIds.split(",");
                    for(roleId in roleIds){
                        var node = $('#roleCheckbox').tree('find', roleIds[roleId]);
                        if(node && node.target){
                            $('#roleCheckbox').tree('check', node.target);
                        }
                    }
                }
            });
        }
    });

    //上传操作
    $('#uploadBtn').bind('click', function(){
        uploadFile('uploadBtn', 'uploadfile', 'fileName', 'fileId', 'url', 'size', 'fix', 'md5', 'showImg');
    })

    //图片回显
    var fileId = $('#fileId').val();
    if(fileId != null && fileId != ""){
        $.ajax({
            url: CONTEXT_PATH + "/commonFile/getFileUrlById.do?id=" + fileId,
            type: "get",
            success: function (data) {
                var url = data.message;
                var imgDiv = $('#showImg');
                if(url != "" && url != null){
                    imgDiv.attr('src',url);
                }
            }
        });
    }

    //操作按钮点击
    $('#optionUser').bind('click', function(){
        doOptionUserMethod();
    })


    //修改时填入文件名称
    var compLogoName = $('#fileName').val();
    if(compLogoName != null && compLogoName != ""){
        $('#uploadfile').filebox({prompt:compLogoName});
    }

});


/**
 * 执行保存操作方法
 */
function doOptionUserMethod(){
    var params = $("#operate_form").serialize();
    $("#operate_form").form("submit",{
        cache:false,
        data: params,
        url: CONTEXT_PATH + "/user/save.do",
        onSubmit:function() {
            getCheckedIds();
            var flag = $(this).form("validate");
            if(flag){
                preventSubmit(null, "optionUser");
            }
            return flag;
        },
        success:function(data) {
            var obj = JSON.parse(data);
            var url = CONTEXT_PATH + "/user/index.do";
            saveJump(obj,url);
        }
    });
}

/**
 * 在修改页面的时候，获取所属公司，部门，项目的值，并给下拉框设置当前选中
 */
function setValue(){
    //获取所属公司值，并给下拉框设置当前选中
    var companyId = $('#companyId').val();
    if(companyId != null && companyId != "" && companyId != "0"){
        $('#companyTree').combotree('setValue', companyId);
    }
    //添加时，默认设置当前用户的公司
    var usId = $("#usId").val();
    if(usId == null || usId == ""){
        var defaultCompanyId = $('#defaultCompanyId').val();
        if(defaultCompanyId != null && defaultCompanyId != "" && defaultCompanyId != "0"){
            $('#companyTree').combotree('setValue', defaultCompanyId);
            $("#companyId").val(defaultCompanyId);
        }
    }

    if(companyId!=null&&companyId!=""){
        //获取所属员工值，并给下拉框设置当前选中
        var emId =$('#emId').val();
        $("#employeeSelect").combobox("clear");
        $.ajax({
            url: CONTEXT_PATH + "/employee/getEmployeeList.do",
            data:{
                "companyId":companyId
            },
            type: "get",
            success: function (data) {
                var themecombo = JSON.parse(data);
                $("#employeeSelect").combobox("loadData",themecombo);
                $("#employeeSelect").combobox({
                    valueField:'id',
                    textField:'text',
                    onChange:function(node){
                        $('#emId').val(node);
                    }
                });
                $("#employeeSelect").combobox("setValue",emId);
            }
    });
    }
}

/**
 * 获得所有选取的Id值
 */
function getCheckedIds(){
    var nodes = $("#roleCheckbox").tree('getChecked');
    //设置所勾选的菜单权限ID，并给页面的menuIds赋值
    var roleIds = "";
    var ids =[];
    for(node in nodes){
        ids.push(nodes[node].id)
    }
    roleIds = ids.join(",");
    $("#roleIds").val(roleIds);
}